from ultralytics import YOLO
import cv2
import dati
from PIL import Image
import j_ocr
import numpy as np
def shi_juan(uid,url):
    model = YOLO(model='best.pt')
    print(url)

    images = []
    img = cv2.imread(url)
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # YOLO可能需要RGB格式的图像
    img_array = Image.fromarray(img_rgb)
    images = [np.array(img_array)]  # 创建一个包含单张图片的列表
    res = dati.A(images, model)
    o=0
    for i in range(0, len(images)):
        o = o+1
        img = images[i]
        data = res[i]
        img_name = []
        for x in data:
            o = o + 1
            id = x["id"]
            x1, y1, x2, y2 = x["xyxy"]
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.putText(img, str(id), (x1 , y1 + 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
            if o==4:
                cv2.putText(img, str(7), (x2-30, y1+35), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
            s = f"{uid}_{id}.jpg"
            cv2.imwrite(s, img[y1:y2, x1:x2])
            img_name.append(s)
        cv2.imwrite(f'{uid}_1000.jpg', img)
        img_name.append(f'{uid}_1000.jpg')
        return j_ocr.shi_bie(img_name)